package com.cobo.fitness.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cobo.fitness.web.remote.mapper.DbMapper;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.EventBus;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Component
public class DbService extends ServiceImpl<DbMapper, Object> {


    static class Query {
        String sql;

        String uuid;


        public Query() {

        }

        public Query(String sql, String uuid) {
            this.sql = sql;
            this.uuid = uuid;
        }

        public String getSql() {
            return sql;
        }

        public void setSql(String sql) {
            this.sql = sql;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
    }


    public List<Map<String, Object>> list(String sql) {

        return getBaseMapper().listBySql(sql);
    }
//
//    @PostConstruct
//    public void start() {
//        VertxOptions options = new VertxOptions();
//        Vertx.clusteredVertx(options, res -> {
//            if (res.succeeded()) {
//                System.out.println("successed");
//                EventBus eventBus = res.result().eventBus();
//                /**
//                 * 执行远程sql查询，并且返回查询后的结果
//                 * */
//                eventBus.consumer("db.query.sql", message -> {
//                    System.out.println("received");
//                    String json = message.body().toString();
//                    Query query = JSON.parseObject(json, Query.class);
//                    List<Map<String, Object>> rets = getBaseMapper().listBySql(query.sql);
//                    eventBus.publish("db.query.sql." + query.uuid, JSON.toJSONString(rets));
//                });
//            } else {
//                System.out.println("failed:" + res.cause());
//            }
//        });
//    }
}
